﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.Text;
using System.IO;

namespace XML
{
	public class Program
	{
		static void Main(string[] args)
		{
			XmlDocument doc = new XmlDocument();
			doc.Load(@"..\..\Book.xml");
			List<BookModel> bmlist = new List<BookModel>();

			XmlNode xn = doc.SelectSingleNode("bookstore");
			XmlNodeList xnl = xn.ChildNodes;
			foreach (XmlNode xn1 in xnl)
			{
				BookModel bm = new BookModel();
				XmlElement xe = (XmlElement)xn1;
				bm.BookISBN = xe.GetAttribute("ISBN").ToString();
				bm.BookType = xe.GetAttribute("Type").ToString();
				XmlNodeList xml0 = xe.ChildNodes;
				bm.BookName = xml0.Item(0).InnerText;
				bm.BookType = xml0.Item(1).InnerText;

				bm.BookPrice = Convert.ToDouble(xml0.Item(2).InnerText);

				bmlist.Add(bm);
				DataContractJsonSerializer json = new DataContractJsonSerializer(bmlist.GetType());

				string szJson = "";

				//序列化

				using (MemoryStream stream = new MemoryStream())
				{

					json.WriteObject(stream, bmlist);

					szJson = Encoding.UTF8.GetString(stream.ToArray());

				}
				Console.WriteLine(szJson);
				Console.ReadLine();

			}
		}
	}
}
